$('#updates').live("pagebeforeshow", function(event) {
	$('#agenda_tab').click();
	agendaList = new updatesList("agenda", $('#updates_agenda_list'));
	addUpdates(agendaList);
	
	vakkenList = new updatesList("vakken", $('#updates_vakken_list'));
	addUpdates(vakkenList);
});
$('div[data-role="navbar"] a').live('click', function () {
	if(!($(this).attr('id') == null))
		localStorage["currentUpdate"] = $(this).attr('id').substring(0,6);
});	
$('#home').live("pagebeforeshow", function(event) {
	homeList = new updatesList("vakken", $('#updates_homepage'));
	homeList.list.append('<li data-role="list-divider" data-theme="b">Updates</li>');
	addUpdates(homeList);
});

function updatesList(type, list) {
	list.empty();
	this.type = type;
	this.list = list;
	this.currentDate;
	this.page = 0;
	if(type == "agenda")
		this.url = "/api/agenda/updates?rpp=30&page=";
	else this.url = "/api/updates?rpp=30&page=";
}

function addUpdates(listObj) {
	$.ajax( { 
		url: listObj.url+listObj.page,
			dataType:"json",
			success: function(data) {
				var updates = data.result;
				var currentDate = listObj.currentDate;
				for(var i=0; i<updates.length; i++) {
					printLine(updates[i],listObj);
				}
				listObj.list.listview("refresh");
				//listObj.currentDate = currentDate;
			},
			error: function(data) {
				console.log("** ERROR: Can not load json object **");
				console.log(data)
			}
	});
}

function printLine(update, listObj) {
	switch(listObj.type) {
	case "agenda":
		dateTime = update.timeStamp;
		break;
	case "vakken":
		dateTime = update.datestamp;
		break;
	}
	date = dateTime.substring(0,11);
	time = dateTime.substring(12,17);
	if(!(date == listObj.currentDate)) {
		listObj.currentDate = date;
		li = $('<li data-role="list-divider" data-theme="a">' + listObj.currentDate + '</li>');
		listObj.list.append(li);
	}
	switch(listObj.type) {
	case "agenda":
		switch(update.type) {
		case "deadline":
			li = $('<li data-theme="e"><img src="images/licons/ti_office_calendar.png"/><p><strong>'+update.vakName+'</strong></p><p style="white-space:normal">'+update.omschrijving+'</p><p>'+update.datum+'</p></li>');
			break;
		case "afspraak":
			if(update.hours == 0 && update.minutes == 0)
				timeString = update.datum;
			else timeString = update.datum.substring(0,16)+'-'+update.einde.substring(11,16);
			li = $('<li><img src="images/licons/kf_emblem_people.png"/><p><strong>'+update.met+'</strong></p><p style="white-space:normal">'+update.omschrijving+'</p><p>'+timeString+'</p></li>');
			break;
		}
		break;
	case "vakken":
		if(update.image == null)
			li = $('<li><img src="images/licons/ic_tab_artists_grey.png" /><p><strong>'+update.author+'</strong></p><p style="white-space:normal">'+update.text+'</p><p>'+time+'</p></li>');
		else li = $('<li><img src='+update.image+' /><p><strong>'+update.author+'</strong></p><p style="white-space:normal">'+update.text+'</p><p>'+time+'</p></li>');
		break;
	}
	listObj.list.append(li);
}